﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.BL;
using CSP.Core;

namespace CSPTest.Admin
{
    public partial class Role : EntityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override EntityDataSource EntitySource
        {
            get { return this.edsMain; }
        }

        protected override TextBox EntitySearchTxt
        {
            get { return this.SearchText; }
        }

        protected override Button EntityAddBtn
        {
            get { return this.BtnAdd; }
        }

        protected override Button EntityClearBtn
        {
            get { return this.BtnClear; }
        }

        protected override GridView EntityGrid
        {
            get { return gvMain; }
        }

        protected override DetailsView EntityDetails
        {
            get { return dvMain; }
        }

        protected override void OnEntityDisabling(int entityKey)
        {
            RoleHelper.OnRoleDisabling(entityKey);
        }


        protected override string GetEntitySortExpression(string keyword)
        {
            return "(it.Name like N'%" + keyword + "%'"
                + " or it.Description like N'%" + keyword + "%')";
        }

        protected override void OnEntityInserting(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var role = e.Entity as Entity.Role;
            var countryList = this.Request.Form["CountryList"];
            var pageList = this.Request.Form["PageList"];
            var belongsTo = this.Request.Form["BelongsTo"];

            RoleHelper.OnRoleInserting(dc, role, countryList, pageList, belongsTo);
        }

        protected override void OnEntityUpdating(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var role = e.Entity as Entity.Role;
            var countryList = this.Request.Form["CountryList"];
            var pageList = this.Request.Form["PageList"];
            var belongsTo = this.Request.Form["BelongsTo"];

            RoleHelper.OnRoleUpdating(dc, role, countryList, pageList, belongsTo);
        }

        protected IEnumerable FilterPages(object source, string belongsTo)
        {
            return from t in source as IEnumerable<Entity.AspxPage>
                   where t.BelongsTo == belongsTo
                   select t;
        }
    }
}
